package thread;
/**
 * 测试多线程并发安全问题
 * */
public class SyncDemo1 {
    private static boolean success1 = false;//表示是否取款成功
    private static boolean success2 = false;

    public static void main(String[] args) {
        Bank bank = new Bank();

        int sum = 0;
        while (true) {
            sum++;

            Thread t1 = new Thread("张三") {
                public void run() {
                    success1 = bank.getMoney(20000);
                }
            };
            Thread t2 = new Thread("李四") {
                public void run() {
                    success2 = bank.getMoney(20000);
                }
            };
            t1.start();
            t2.start();

            try {
                //让主线程阻塞5毫秒，目的是等待t1,t2线程执行完毕
                Thread.sleep(5);
            } catch (InterruptedException e) {
            }
            if (success1 && success2) {
                System.out.println("两个人都取款成功！出现了问题！");
                System.out.println("共"+sum+"次");
                break;
            } else {
                bank.saveAccount(20000);//重新存上
           //     System.out.println("一切正常");
            }

        }

    }
}
